import './style/index.less'
import  './utils/ui-toast'
import  './utils/ajax'
import  {getUrlParam} from './utils/base'
import MiniRefreshTools from 'minirefresh';
import 'minirefresh/dist/debug/minirefresh.css'

(function () {

    let id='3836894329100566528',page=0;
    if(getUrlParam('id')){
        id=getUrlParam('id')
    }

    //下拉
    var miniRefresh = new MiniRefresh({
        container: '#minirefresh',
        down: {
            callback: function() {
                init('down')
            }
        },
        up: {
            isAuto:true,
            callback: function() {
                init('up')
            },
            contentnomore:'全部加载完'
        }
    });

    //初始化数据
    function init(type){
        var str='';
        if(type==='down'){
            page=1
        }else{
            page++
        }
        getJSON(`/classbuddy/api/v1/lesson/readPage?id=${id}&page=${page}`, function(result) {
            if(result.head.returnCode==0){
                if(result.head.subCode==0){
                    if(result.body){
                        if(!result.body.content || result.body.content===''){
                            if(type==='down'){
                                document.querySelector('.lesson_content').innerHTML='';
                                miniRefresh.endDownLoading(true);
                            }else {
                                miniRefresh.endUpLoading(true);
                          }
                        }else {
                            str+=result.body.content;
                            setTimeout(function() {
                                if(type==='down'){
                                    document.querySelector('.lesson_content').innerHTML='';
                                    document.querySelector('.lesson_content').append(str);
                                    miniRefresh.endDownLoading(true);
                                }else {
                                    document.querySelector('.lesson_content').append(str);
                                    miniRefresh.endUpLoading(false);
                                }
                            }, 200);
                        }
                        document.querySelector('.title').innerHTML=result.body.title||'';
                        document.querySelector('.teacher').innerHTML=result.body.teacher||'';
                        document.querySelector('.email').innerHTML=result.body.email||'';
                    }else {
                        toast('暂无信息');
                    }

                }else {
                    toast(result.head.subMsg);
                    miniRefresh.endUpLoading(true);
                }
            }else {
                toast(result.head.returnMsg);
                miniRefresh.endUpLoading(true);
            }
        });
    }
     // init('down')
})()





